package sg.com.techstudio.ecc.excel;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import sg.com.techstudio.ecc.controller.BaseController;
import sg.com.techstudio.ecc.obj.ParticipantObj;
import sg.com.techstudio.ecc.obj.RegisterObj;

@Component("reportRegistrationExcel")
public class ReportRegistrationExcel extends BaseExcel {

	private static final Log log = LogFactory.getLog(ReportRegistrationExcel.class);
	
	@Override
	protected String getFilename() {
		return "registration_"+FILENAME_SUFFIX_SDF.format(Calendar.getInstance().getTime())+EXCEL_EXTENSION;
	}

	@Override
	protected String getTabname() {
		return "registration";
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			WritableWorkbook wb, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		log.debug("buildExcelDocument() entry");
		
		processServletRequestResponse(req, resp);
        WritableSheet ws = prepareWritableSheet(wb);

        int row = 0, col=0;
        List<RegisterObj> regList = (List<RegisterObj>) model.get(BaseController.PARAM_CONTENT_LIST);

        writeColumnHeader(ws, row++, col, new String[]{"Reg Id", "Participant Id", "Register Date","Updated Date"
        		,"Salutation", "Name", "NRIC", "Badge Name", "Designation", "Organization", "Mobile", "Office No"
        		, "Email", "Address", "Postal Code", "Vegetarian"
        		, "Session A", "Session B", "Session C", "Session D"
        		, "Amount", "Paypal Payment status", "Paypal payer"});
        if(regList!=null)
        for(RegisterObj r: regList) {
        	if(r.getRegType()>0)
        		continue;
        	col=0;
        	ws.addCell(new Number(col++, row, r.getId()));
        	col++; //participant id
        	if(r.getCreatedDate()==null)
        		col++;
        	else
        		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getCreatedDate())));
        	if(r.getUpdatedDate()==null)
        		col++;
        	else
        		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getUpdatedDate())));
        	ws.addCell(new Label(col++, row, r.getSalutation()));
        	ws.addCell(new Label(col++, row, r.getName()));
        	ws.addCell(new Label(col++, row, r.getNric()));
        	ws.addCell(new Label(col++, row, r.getBadgeName()));
        	ws.addCell(new Label(col++, row, r.getDesignation()));
        	ws.addCell(new Label(col++, row, r.getCompany()));
        	ws.addCell(new Label(col++, row, r.getMobile()));
        	ws.addCell(new Label(col++, row, r.getOfficeNo()));
        	ws.addCell(new Label(col++, row, r.getEmail()));
        	ws.addCell(new Label(col++, row, r.getAddress()));
        	ws.addCell(new Label(col++, row, r.getPostalCode()));
        	ws.addCell(new Label(col++, row, r.getVegetarian()!=null&&r.getVegetarian()==1?"Y":"N"));
        	ws.addCell(new jxl.write.Number(col++, row, r.getSessionA()==null?0:r.getSessionA()));
        	ws.addCell(new jxl.write.Number(col++, row, r.getSessionB()==null?0:r.getSessionB()));
        	ws.addCell(new jxl.write.Number(col++, row, r.getSessionC()==null?0:r.getSessionC()));
        	ws.addCell(new jxl.write.Number(col++, row, r.getSessionD()==null?0:r.getSessionD()));
        	ws.addCell(new jxl.write.Number(col++, row, r.getAmount()==null?0:r.getAmount(), dollarFormat()));
        	ws.addCell(new Label(col++, row, paymentStatus(r.getStatus())));
        	ws.addCell(new Label(col++, row, r.getPayerEmail()));
        	row++;
        	
        	if(r.getRegType()>0 || r.countParticipants()<=0)
        		continue;
        	for(ParticipantObj p: r.getParticipantList()) {
        		col=0;
            	ws.addCell(new Number(col++, row, r.getId()));
            	ws.addCell(new Number(col++, row, p.getId()));
            	if(p.getCreatedDate()==null)
            		col++;
            	else
            		ws.addCell(new DateTime(col++, row, gmtDateTime(p.getCreatedDate())));
            	col++; //update date
            	ws.addCell(new Label(col++, row, p.getSalutation()));
            	ws.addCell(new Label(col++, row, p.getName()));
            	ws.addCell(new Label(col++, row, p.getNric()));
            	ws.addCell(new Label(col++, row, p.getBadgeName()));
            	ws.addCell(new Label(col++, row, p.getDesignation()));
            	ws.addCell(new Label(col++, row, r.getCompany()));
            	ws.addCell(new Label(col++, row, p.getMobile()));
            	ws.addCell(new Label(col++, row, r.getOfficeNo()));
            	ws.addCell(new Label(col++, row, p.getEmail()));
            	ws.addCell(new Label(col++, row, r.getAddress()));
            	ws.addCell(new Label(col++, row, r.getPostalCode()));
            	ws.addCell(new Label(col++, row, p.getVegetarian()!=null&&p.getVegetarian()==1?"Y":"N"));
            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionA()==null?0:p.getSessionA()));
            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionB()==null?0:p.getSessionB()));
            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionC()==null?0:p.getSessionC()));
            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionD()==null?0:p.getSessionD()));
            	col++;
            	ws.addCell(new Label(col++, row, paymentStatus(p.getStatus())));
            	row++;
        	}
        }
        
//        ws = prepareWritableSheet(wb, "participant", 1);
//        row = col= 0;
//        writeColumnHeader(ws, row++, col, new String[]{"Participant Id","Main Register Id", "Register Date"
//        		,"Salutation", "Name", "NRIC", "Badge Name", "Designation", "Mobile"
//        		, "Email", "Vegetarian"
//        		, "Session A", "Session B", "Session C", "Session D"
//        		, "Paypal Payment status"});
//        if(regList!=null)
//        for(RegisterObj r: regList) {
//        	if(r.getRegType()>0 || r.countParticipants()<=0)
//        		continue;
//        	for(ParticipantObj p: r.getParticipantList()) {
//        		col=0;
//            	ws.addCell(new Number(col++, row, p.getId()));
//            	ws.addCell(new Number(col++, row, r.getId()));
//            	if(p.getCreatedDate()==null)
//            		col++;
//            	else
//            		ws.addCell(new DateTime(col++, row, gmtDateTime(p.getCreatedDate())));
//            	ws.addCell(new Label(col++, row, p.getSalutation()));
//            	ws.addCell(new Label(col++, row, p.getName()));
//            	ws.addCell(new Label(col++, row, p.getNric()));
//            	ws.addCell(new Label(col++, row, p.getBadgeName()));
//            	ws.addCell(new Label(col++, row, p.getDesignation()));
//            	ws.addCell(new Label(col++, row, p.getMobile()));
//            	ws.addCell(new Label(col++, row, p.getEmail()));
//            	ws.addCell(new Label(col++, row, p.getVegetarian()!=null&&p.getVegetarian()==1?"Y":"N"));
//            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionA()==null?0:p.getSessionA()));
//            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionB()==null?0:p.getSessionB()));
//            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionC()==null?0:p.getSessionC()));
//            	ws.addCell(new jxl.write.Number(col++, row, p.getSessionD()==null?0:p.getSessionD()));
//            	ws.addCell(new Label(col++, row, paymentStatus(p.getStatus())));
//            	row++;
//        	}
//        }
       
        ws = prepareWritableSheet(wb, "learning journey", 1);
        row = col= 0;
        writeColumnHeader(ws, row++, col, new String[]{"Reg Id", "Register Date","Updated Date"
        		,"Salutation", "Name", "NRIC", "Designation", "Organization", "Mobile", "Office No"
        		, "Email", "Address", "Postal Code"
        		, "Learning Journey"
        		, "Amount", "Paypal Payment status", "Paypal payer"});
        if(regList!=null)
            for(RegisterObj r: regList) {
            	if(r.getRegType()<1)
            		continue;
            	col=0;
            	ws.addCell(new Number(col++, row, r.getId()));
            	if(r.getCreatedDate()==null)
            		col++;
            	else
            		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getCreatedDate())));
            	if(r.getUpdatedDate()==null)
            		col++;
            	else
            		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getUpdatedDate())));
            	ws.addCell(new Label(col++, row, r.getSalutation()));
            	ws.addCell(new Label(col++, row, r.getName()));
            	ws.addCell(new Label(col++, row, r.getNric()));
            	ws.addCell(new Label(col++, row, r.getDesignation()));
            	ws.addCell(new Label(col++, row, r.getCompany()));
            	ws.addCell(new Label(col++, row, r.getMobile()));
            	ws.addCell(new Label(col++, row, r.getOfficeNo()));
            	ws.addCell(new Label(col++, row, r.getEmail()));
            	ws.addCell(new Label(col++, row, r.getAddress()));
            	ws.addCell(new Label(col++, row, r.getPostalCode()));
            	ws.addCell(new jxl.write.Number(col++, row, r.getLearningJourney()));
            	ws.addCell(new jxl.write.Number(col++, row, r.getAmount()==null?0:r.getAmount(), dollarFormat()));
            	ws.addCell(new Label(col++, row, paymentStatus(r.getStatus())));
            	ws.addCell(new Label(col++, row, r.getPayerEmail()));
            	row++;
            }
        
        log.debug("buildExcelDocument() exit");
	}
	
	protected String paymentStatus(int paymentStatus) {
		switch(paymentStatus) {
		case 1:
			return "Completed";
		}
		return "";
	}
	
}
